package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.common.R;
import com.itheima.dao.CategoryDao;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

   private static  final Logger logger = LogManager.getLogger(CategoryServiceImpl.class);

    /**
     * 根据id删除分类，删除之前需要进行判断
     *
     * @param id
     */
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;


    //方式一
    /*@Override
    public void remove(Long id) {

        //添加查询条件，根据分类id进行查询菜品数据
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<Dish>();
        queryWrapper.eq(Dish::getCategoryId,id);  //查询category主表id 与 Dish 从表 category_id相等的条数
        int count1 = dishService.count(queryWrapper);

        //如果已经关联，抛出一个业务异常
        if(count1 > 0 ){
            throw new CustomException("当前分类下关联了菜品，无法删除");
        }

        //查询当前分类是否关联了套餐，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<Setmeal>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);//查询category主表id 与 Sermeal 从表 Sermeal_id相等的条数
        int count2 = setmealService.count(setmealLambdaQueryWrapper);

        //如果已经关联，抛出一个业务异常
        if (count2 > 0 ){
            throw  new CustomException("当前分类下关联了套餐，无法删除");
        }

        //正常删除分类
        super.removeById(id);
    }*/


    //方式二
    @Override
    public R<String> remove(Long id) {
        //条件构造器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //具体条件
        lqw.eq(Category::getId, id);
        //查询
        Category category = super.getById(id);
        //判断
        if (null == category) return R.error("套餐或菜品对象为空");
        if (null == category.getType()) return R.error("没有分类，类型错误！");
        //统计
        int count = 0;
        //具体类型查询
        if (1 == category.getType()) {
            LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Dish::getCategoryId,id);
            count = dishService.count();

        }else if(2 == category.getType()){
            LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Setmeal::getCategoryId,id);
            count = setmealService.count();
        }

        if(count>0)return R.error("关联了套餐或菜品，无法删除");
        //删除
        super.removeById(id);
        return R.success("删除成功！");
    }
}
